﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_SaleProcessHealthForm
 * 类型：       控制层
 * 功能说明：   销售过程-体检报告页面
 * 作者：       章 昕
 * 创建日期：   2007/08/30
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Commons.Db.Beans.Inp;
using Shpm.App.Commons.Utils;
using Shpm.System.Web.Control;
using log4net;
using System.Text.RegularExpressions;
using Shpm.System;
using Shpm.App.Commons.Utils;

public partial class App_Input_SaleProcessHealthForm : System.Web.UI.Page
{
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_SaleProcessHealthForm));

    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                // 流ID
                this.ViewState["PROC_ID"] = "";
                // 流取消Flg
                this.ViewState["CANCEL_FLG"] = null;
                //// 流当前步序
                //this.ViewState["CUR_STEP"] = null;

                if (Context.Items["SALE_PROC_ID"] != null)
                {
                    this.ViewState["PROC_ID"] = Convert.ToString(Context.Items["SALE_PROC_ID"]);
                }

                // load exist data
                Boolean blnFindData = false;
                if (this.ViewState["PROC_ID"].ToString().Length > 0)
                {
                    blnFindData = bindSelectData(this.ViewState["PROC_ID"].ToString());
                }

                // has authority ？ is 审核画面menu show        
                SysUserManager manager = new SysUserManager(Context);
                SysUserBean sysUserBean = manager.getSysUserBean();
                MstSysUserBean mstSysUserBean =
                    sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
                MstEmployeeBean mstEmpBean =
                    sysUserBean == null ? null : sysUserBean.getEmployeeBean();
                string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
                string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
                string emplyRole = mstEmpBean == null ? "" : mstEmpBean.Role;
                if (!SaleProcessDao.ROLE_MANAGER.Equals(emplyRole)) LinkButton10.Enabled = false;

                if (Context.Items.Contains("RIGHT")) ViewState["RIGHT"] = Context.Items["RIGHT"];

            }
        }
        catch (Exception ex)
        {
            log.Fatal(ex.StackTrace);
            log.Fatal(ex.Message);
            throw ex;
        }
    }

    // before render
    void Page_PreRender(object sender, EventArgs e)
    {
        // 销售过程跟踪 link 状态
        //setLinkStatus();
    }

    // 登记按钮
    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            // check is allow update
            if (!ComValidator1.DoValidator())
            {
                ((Design_MasterPage)this.Master).InfoMessage = ComValidator1.ErrorMessage;
                return;
            }

            SaleProcessDao dao = new SaleProcessDao();
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
            string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
            string emplyRole = mstEmpBean == null ? "" : mstEmpBean.Role;

            // 权限check
            if (!checkAuthority(this.ViewState["PROC_ID"].ToString(), emplyId, emplyRole))
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0901").ToString();
                return;
            }

            // find current process
            SaleProcessBean beanSP = new SaleProcessBean();
            beanSP.SaleProcId = Convert.ToInt32(this.ViewState["PROC_ID"].ToString());
            beanSP = dao.FindBySaleProcess(beanSP);

            if (beanSP == null)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0005").ToString();
                return;
            }

            // 判断是否已经审核
            if (beanSP.CancelFlg)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0004").ToString();
                return;
            }

            // 判断是否已经入会登记
            if (beanSP.PersonId == null)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0902").ToString().Replace("{0}", "人员登记");
                return;
            }

            // check physical id exist
            InpPhyscalBean beanPhy = null;
            // 主会员
            if (beanSP.PersonId != null)
            {
                beanPhy = new InpPhyscalBean();
                beanPhy.PhysicalId = Convert.ToInt32(txtPhysicalIdMain.Text);
                beanPhy.PersonId = beanSP.PersonId.Value;
                IList listPhy = dao.CheckPhysicalExt(beanPhy);
                if (listPhy == null || listPhy.Count == 0)
                {
                    ((Design_MasterPage)this.Master).InfoMessage =
                        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0006").ToString().Replace("{0}", Convert.ToString(beanSP.PersonId));
                    return;
                }
            }
            // 副会员
            if (beanSP.SecondaryPersonId != null)
            {
                beanPhy = new InpPhyscalBean();
                if (!string.IsNullOrEmpty(txtPhysicalIdAsst.Text))
                { beanPhy.PhysicalId = Convert.ToInt32(txtPhysicalIdAsst.Text); }
                beanPhy.PersonId = beanSP.SecondaryPersonId.Value;
                IList listPhy = dao.CheckPhysicalExt(beanPhy);
                if (listPhy == null || listPhy.Count == 0)
                {
                    ((Design_MasterPage)this.Master).InfoMessage =
                        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0006").ToString().Replace("{0}", Convert.ToString(beanSP.SecondaryPersonId));
                    return;
                }
            }
            // 陪住人
            if (beanSP.AccompanyPersonId != null)
            {
                beanPhy = new InpPhyscalBean();
                if (!string.IsNullOrEmpty(txtPhysicalIdComp.Text))
                { beanPhy.PhysicalId = Convert.ToInt32(txtPhysicalIdComp.Text); }
                beanPhy.PersonId = beanSP.AccompanyPersonId.Value;
                IList listPhy = dao.CheckPhysicalExt(beanPhy);
                if (listPhy == null || listPhy.Count == 0)
                {
                    ((Design_MasterPage)this.Master).InfoMessage =
                        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0006").ToString().Replace("{0}", Convert.ToString(beanSP.AccompanyPersonId));
                    return;
                }
            }

            // 更新
            beanSP.PhysicalDate = DateTime.Now;
            beanSP.PhysicalId = Convert.ToInt32(txtPhysicalIdMain.Text);
            if (!string.IsNullOrEmpty(txtPhysicalIdAsst.Text))
            {
                beanSP.SecondaryPhysicalId = Convert.ToInt32(txtPhysicalIdAsst.Text);
                beanSP.SecondaryPhysicalDate = DateTime.Now;
            }
            if (!string.IsNullOrEmpty(txtPhysicalIdComp.Text))
            {
                beanSP.AccompanyPhysicalId = Convert.ToInt32(txtPhysicalIdComp.Text);
                beanSP.AccompanyPhysicalDate = DateTime.Now;
            }

            beanSP.AuditingFlg = ckbAuditing.Checked;
            beanSP.UserId = loginId;
            log.Info("***** Start UPDATE *****");
            dao.Update(beanSP);
            this.ViewState["CUR_STEP"] = beanSP.CurrentStep;
            ((Design_MasterPage)this.Master).InfoMessage =
                HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();

        }
        catch (Exception ex)
        {
            log.Fatal(ex.StackTrace);
            log.Fatal(ex.Message);
            throw ex;
        }
    }

    // link 按钮 click
    protected void lnkBtn_Command(object sender, CommandEventArgs e)
    {
        string directUrl = "~/App/Search/SaleProcSrhForm.aspx";
        switch (e.CommandName)
        {
            case "btn0":
                // 销售开始                
                directUrl = "~/App/Input/SaleProcessBeginForm.aspx";
                break;
            case "btn1":
                // 入会登记
                directUrl = "~/App/Input/SaleProcesseMembershipForm.aspx";
                break;
            case "btn2":
                // 体检报告
                directUrl = "~/App/Input/SaleProcessHealthForm.aspx";
                break;
            case "btn3":
                // 资格审核
                directUrl = "~/App/Input/SaleProcessIndefinForm.aspx";
                break;
            case "btn4":
                // 合同签订
                directUrl = "~/App/Input/SaleContractForm.aspx";
                break;
            case "btn5":
                // 费用缴纳
                directUrl = "~/App/Input/PaymentUpdForm.aspx";
                break;
            case "btn6":
                // 礼品发放
                directUrl = "~/App/Search/SaleProcSrhForm.aspx";
                break;
            case "btn7":
                // 完成
                directUrl = "~/App/Search/SaleProcSrhForm.aspx";
                break;
            default:
                break;
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["PROC_ID"])))
        {
            Context.Items["SALE_PROC_ID"] = this.ViewState["PROC_ID"];
        }
        ControllerUtils.Transfer(Context, directUrl, true);
    }

    // 是否有更新权限 check
    private Boolean checkAuthority(string procId, string emplyId, string role)
    {
        SaleProcessDao daoCheck = new SaleProcessDao();
        SaleProcessBean bean = new SaleProcessBean();
        bean.SaleProcId = Convert.ToInt32(procId);
        bean.EmployeeId = emplyId;
        bean.Role = role;
        IList list = daoCheck.CheckSaleProcessAu(bean);
        if (list == null || list.Count == 0) return false;
        return true;
    }

    // bind select data by procId
    private Boolean bindSelectData(string procId)
    {
        SaleProcessDao dao = new SaleProcessDao();
        SaleProcessBean beanSP = new SaleProcessBean();
        beanSP.SaleProcId = Convert.ToInt32(procId);
        beanSP = dao.FindBySaleProcess(beanSP);
        if (beanSP == null) return false;

        lblProcId.Text = beanSP.SaleProcId.ToString();
        lblEmpId.Text = string.Format("{0} {1}", beanSP.EmployeeId, beanSP.EmployeeName);
        this.ViewState["CANCEL_FLG"] = beanSP.CancelFlg;
        //this.ViewState["CUR_STEP"] = beanSP.CurrentStep;

        // add by zx 2007/10/08        
        // 判断是否已经入会登记 
        if (beanSP.PersonId == null)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0902").ToString().Replace("{0}", "人员登记");
            btnOk.Enabled = false;
            return false;
        }

        Label1.Text = Convert.ToString(beanSP.PersonId);
        Label2.Text = Convert.ToString(beanSP.SecondaryPersonId);
        Label3.Text = Convert.ToString(beanSP.AccompanyPersonId);
        lblPersonMain.Text = beanSP.PersonNameMain;//string.Format("{0} {1}", Convert.ToString(beanSP.PersonId), Convert.ToString(beanSP.PersonNameMain));
        lblPersonAsst.Text = beanSP.PersonNameAsst;//string.Format("{0} {1}", Convert.ToString(beanSP.SecondaryPersonId), Convert.ToString(beanSP.PersonNameAsst));
        lblPersonComp.Text = beanSP.PersonNameComp;//string.Format("{0} {1}", Convert.ToString(beanSP.AccompanyPersonId), Convert.ToString(beanSP.PersonNameComp));
        txtPhysicalIdMain.Text = Convert.ToString(beanSP.PhysicalId);
        txtPhysicalIdAsst.Text = Convert.ToString(beanSP.SecondaryPhysicalId);
        txtPhysicalIdComp.Text = Convert.ToString(beanSP.AccompanyPhysicalId);
        txtPhysicalIdMain.ReadOnly = (beanSP.PersonId == null) ? true : false;
        txtPhysicalIdAsst.ReadOnly = (beanSP.SecondaryPersonId == null) ? true : false;
        txtPhysicalIdComp.ReadOnly = (beanSP.AccompanyPersonId == null) ? true : false;
        btnRptSrh1.Enabled = !txtPhysicalIdMain.ReadOnly;
        btnRptSrh2.Enabled = !txtPhysicalIdAsst.ReadOnly;
        btnRptSrh3.Enabled = !txtPhysicalIdComp.ReadOnly;

        if (beanSP.AuditingFlg != null) ckbAuditing.Checked = beanSP.AuditingFlg.Value;
        ckbAuditing.Enabled = !beanSP.CancelFlg;

        return true;
    }

    // set link button status
    //private void setLinkStatus()
    //{
    //    // page is allow edit
    //    if (this.ViewState["PROC_ID"] == null
    //        //|| this.ViewState["CUR_STEP"] == null
    //        || this.ViewState["CANCEL_FLG"] == null
    //        || (Boolean)this.ViewState["CANCEL_FLG"])
    //    {
    //        btnOk.Enabled = false;
    //        ((Design_MasterPage)this.Master).InfoMessage =
    //            HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0005").ToString();
    //    }
    //    //// 判断当前步序 是否允许登记
    //    //if (string.IsNullOrEmpty(Convert.ToString(this.ViewState["CUR_STEP"]))
    //    //    || Convert.ToInt32(this.ViewState["CUR_STEP"]) >= SaleProcessDao.SALE_PROCESS_STEP_3)
    //    //{
    //    //    ((Design_MasterPage)this.Master).InfoMessage =
    //    //        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0004").ToString();
    //    //    btnOk.Enabled = false;
    //    //}
    //    //// 判断是否已经入会登记
    //    //if (Convert.ToInt32(this.ViewState["CUR_STEP"]) < SaleProcessDao.SALE_PROCESS_STEP_1
    //    //    || lblPersonMain.Text == string.Empty)
    //    //{
    //    //    ((Design_MasterPage)this.Master).InfoMessage =
    //    //        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0902").ToString().Replace("{0}", "人会登记");
    //    //    btnOk.Enabled = false;
    //    //}

    //    // 流程跟踪 link button
    //    if (this.ViewState["CUR_STEP"] != null)
    //    {
    //        for (int i = 0; i <= Convert.ToInt32(this.ViewState["CUR_STEP"]); i++)
    //        {
    //            if ((i + 1) < 8)
    //                ((LinkButton)this.Master.FindControl("ContentPlaceHolder1").FindControl("lnkBtn" + (i + 1).ToString())).Enabled = true;
    //        }
    //    }
    //}

    // bind db data to selected controls
    private void bindDataToListControl(string ControlId, IList LstData)
    {
        if (LstData == null || LstData.Count == 0) return;

        foreach (CommonDictionaryBean bean in LstData)
        {
            if (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is DropDownList)
            {
                ((DropDownList)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
            else if ((this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is CheckBoxList)
                || (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is RadioButtonList))
            {
                ((ListControl)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
        }
    }

    // tab go
    protected void lnkTab_Command(object sender, CommandEventArgs e)
    {

        string directUrl = "~/App/Input/MemberForm.aspx";
        switch (e.CommandName)
        {
            case "btn0":
                // 新建洽谈
                directUrl = "~/App/Input/SaleProcessBeginForm.aspx";
                break;
            case "btn1":
                // 销售管理
                directUrl = "~/App/Input/SaleCountForm.aspx";
                break;
            case "btn2":
                // 入住管理
                directUrl = "~/App/Input/MemberForm.aspx";
                break;
            case "btn3":
                // 购卡管理
                directUrl = "~/App/Input/RightPersonForm.aspx";
                break;
            case "btn4":
                // 客户移交
                directUrl = "~/App/Input/ContractHabitationForm.aspx";
                break;
            case "btn2_1":
                // 主会员信息
                directUrl = "~/App/Input/MemberForm.aspx";
                break;
            case "btn2_2":
                // 副会员信息
                directUrl = "~/App/Input/ViceMemberForm.aspx";
                break;
            case "btn2_3":
                // 陪住人信息
                directUrl = "~/App/Input/AccompanyForm.aspx";
                break;
            case "btn2_4":
                // 体检报告
                directUrl = "~/App/Input/SaleProcessHealthForm.aspx";
                break;
            case "btn2_5":
                // 审核
                directUrl = "~/App/Input/SaleProcessIndefinForm.aspx";
                break;
            case "btn2_6":
                // 入住合同
                directUrl = "~/App/Input/ContractHabitationForm.aspx";
                break;
            case "btn2_7":
                // 特点
                directUrl = "~/App/Input/PersonTraitUpdForm.aspx";
                break;
            case "btn6":
                // 资格审核
                directUrl = "~/App/Input/SaleProcessIndefinForm.aspx";
                break;
            default:
                break;
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["PROC_ID"])))
        {
            Context.Items["SALE_PROC_ID"] = this.ViewState["PROC_ID"];
        }
        ControllerUtils.Transfer(this.Context, directUrl, false);
    }
}
